package com.dianban.utils;

import com.alibaba.fastjson.JSONObject;
import com.dianban.utils.http.HttpClient;
import com.dianban.utils.http.HttpMethod;
import com.dianban.utils.http.HttpParamers;
import com.dianban.utils.http.RSAUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.*;

public class XiaomaiguiUtils {
    private static Logger logger = LoggerFactory.getLogger(XiaomaiguiUtils.class);
    public static final String APPID = "7009013ce4f4fe6dee54211571cc3198";
    private static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlxT7wtNK6vWK50jmkXo6rcDjorogj1uYkKGFWhbHnynFa1GVxBIpz60JRhwHwRznCQQ/9wOEaD9LDve258m6fxe3y5e7Fam3Hx1GM9xTynbLsZZud1TNAUTHBxjozxfXxkJEwUfbcMPtynt/qMfOdktANZH95SmeckDdW7AGKfdCNf3bJuq+PEzpc4UJ+zH5bHVZWS8Hc07glcg79nv/zrsqqRjaJYiD93UV7Wq+hLCiuCijFg91RfwA7ncQvFTvLGiHE7kxXUpQanhvjNZ69mJnHbD5H2nYDzLJy5/GgPtvg9neWo/hTwU4kEci5ztYSvjmjPcUbf92SZKwS5IfUwIDAQAB";
    private static final String PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDOqXIIdH5U6t9O" +
            "xO7vMWaetA6g4iJPkEE7R+xdk8x6sc0+y6ilhtAWygp+P0K3461/Y/QVcFLfLso2" +
            "5AcIsWTqsJ0g0DdUo28RMPzpnFjKDXj/J0XBffvjow7uxxMN7mTD2QTfv9AD3sNB" +
            "0aDpe7OiGsF0A9BOjSzgKt1MWsEPMUZ25UgC+Dj8u2+rmlLI8p2tfcuJPMTI1q+1" +
            "68193IzAoOtJGxFqfAYbawTFgczxpsBRpESpag9rXW3bXxfRdnnDrwm0UcKrCNia" +
            "hrTTcyCvUHRItBiwav1mZ7KbecHWzO98YXhcDHlFMxfcTzNO7U+tz4Cgb4lQLbYK" +
            "6/uT0wnZAgMBAAECggEBAJFUOXvWqCyBSu4ysTAGFntOdecPfz9iW20bxA1+bVJv" +
            "DL23LG7S4izUghKL3BhDBPRLzw7wgWavmcaGDqg/T493CdhUdESR9f4p7AFAUHQu" +
            "i0MrU/Og6+0F8u9ZkX2/gp7zU2khk1/VXIxBpZ3rvsQleptiPy+uObueiSczvwY1" +
            "xJUQDcZqSL+44apFhQSMFw3lBHNuxM6l83dtLJH8cTseqx0yYudcM2cb3etcq+j3" +
            "O/w40qzdWUow5NR6LlYiO4tw+FYrZJtCE6noFsMF7yjfSrcaSQXaOQ/M3dxD7sMW" +
            "HVpQeq0hh2dMicFr2Xj4MFPHZUVHrAROxnEFXxsyRW0CgYEA/gfxw0FQjgs3uCJ0" +
            "2rvjFa+33kwWbghQK79bAZp9b6trSdUzoAIfW9hWJC1nyBlX0XzfTBmkMWGxH7eP" +
            "PoNB67ndVh14gW9/OlRvLOSEtZukTX6NyCEa8ognbjybKqdpC/thjVvrjLLeHidj" +
            "Z9+ox+c9FDC5e+GS1l4cBeZSvdcCgYEA0EOCholzuYexmi2gO2YUcwiQyGoH1izB" +
            "OmfSl45sWtZGcYhyu34E6yrIo4TRZSYlTPbcGIUE1EIssPCCLxRgqTmUzQdwSHpU" +
            "bUDpJqctX9XOoFjFvs2sjoDr0he/hHGDoDkM6RYXPq1AGbrYNwOE74QBfs1TNGjZ" +
            "+Jmk4da1n88CgYEAvp8DgftPEWfFhf0FJuX6f1l+Zec+pY7DajCLf+aH3fYJ6jt8" +
            "I8Oz+F2cNkWELRknVuGXRimvlgayvRQm/0fLfbHZLzeqJ6adGA4n5s4L76M9ZZFj" +
            "nxuIdxpITGZaIWXCY+VczJB/bvMBzirMYzPoGSmiIbZ2WUlpwEaaTKsKx70CgYBv" +
            "vBiWR75vxq5xkpMJ6z2DbhAHhmOH8+EDjdo9d6sInTeUB+nOT9EhvoWAvWx+gRW4" +
            "EiWJfixWGN7TW2XtDljB0ePUkG4cZI1RRGcSVtUQOqyt+JK3SK+QkPh0cktNKn29" +
            "A366uyBjfx4J7+W6tkenHUKUHmLKloqDRyNgCwrMnwKBgCBuAGe7M10+V8SifFf7" +
            "Eu1XTXTwy2nP3ggwm/XvGUVUD6v35c7nKOqqeT2RT6OwO6o5ZAtIyIikRRcozKz9" +
            "oWm6bIY0+1gKMGXsp+M/rtmK0esP/yR8ou1WjPSJdCB6dueT4Ta+Zn+KKPYmGm21" +
            "DlUM69ESNZwcKcyREFNyUHfR";

    public static JSONObject open(String containerid, String orderNo, String mobile) {
        String url = "https://api.xiaomaigui.com/Rscansub/receiveReqData.html";
        JSONObject jsonObject = new JSONObject();
        try {
            long timestamp = System.currentTimeMillis() / 1000;
            HttpParamers httpParamers = new HttpParamers(HttpMethod.POST);
            httpParamers.addParam("mobile", mobile);
            httpParamers.addParam("source", "2");
            httpParamers.addParam("orderNo", orderNo);
            httpParamers.addParam("containerid", containerid);
            httpParamers.addParam("timestamp", timestamp);
            httpParamers.addParam("appid", APPID);
            httpParamers.addParam("method", "open");
            // 加密规则
            String ciphertext = "{\"timestamp\":" + timestamp + ",\"appid\":\"" + APPID + "\"}";
            ciphertext = RSAUtil.encrypt(ciphertext, PUBLIC_KEY);
            httpParamers.addParam("ciphertext", ciphertext);
            httpParamers.setJsonParamer();
            String resultService = HttpClient.doService(url, httpParamers, null);
            logger.info("小卖柜开门接口返回信息-{}", resultService);
            if (StringUtils.isNotBlank(resultService)) {
                jsonObject = JSONObject.parseObject(resultService);
            }
        } catch (Throwable e) {
            logger.info("小卖柜开门接口返回信息-{}", e.getMessage());
        }
        return jsonObject;
    }

    public static JSONObject orderNotify(String containerid, String orderNo, String thirdOrderNo, BigDecimal payfee, String payTime) {
        String url = "https://api.xiaomaigui.com/Rscansub/receiveReqData.html";
        JSONObject jsonObject = new JSONObject();
        try {
            long timestamp = System.currentTimeMillis() / 1000;
            HttpParamers httpParamers = new HttpParamers(HttpMethod.POST);
            httpParamers.addParam("orderNo", orderNo);
            httpParamers.addParam("containerid", containerid);
            httpParamers.addParam("timestamp", timestamp);
            List<Map<String, Object>> orderpaydetail = new ArrayList<>();
            Map<String, Object> order = new HashMap<>();
            order.put("serialno", orderNo);
            order.put("batchno", thirdOrderNo);
            // 单位分
            order.put("payfee", payfee.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_DOWN));
            order.put("paytime", payTime);
            order.put("paystatus", 1);
            order.put("paytype", 2);
            orderpaydetail.add(order);
            httpParamers.addParam("orderpaydetail", orderpaydetail);
            httpParamers.addParam("appid", APPID);
            httpParamers.addParam("method", "order");
            // 加密规则
            String ciphertext = "{\"timestamp\":" + timestamp + ",\"appid\":\"" + APPID + "\"}";
            ciphertext = RSAUtil.encrypt(ciphertext, PUBLIC_KEY);
            httpParamers.addParam("ciphertext", ciphertext);
            httpParamers.setJsonParamer();
            String resultService = HttpClient.doService(url, httpParamers, null);
            logger.info("小卖柜扣款成功通知返回信息-{}", resultService);
            if (StringUtils.isNotBlank(resultService)) {
                jsonObject = JSONObject.parseObject(resultService);
            }
        } catch (Throwable e) {
            logger.info("小卖柜扣款成功通知返回信息-{}", e.getMessage());
        }
        return jsonObject;
    }

    public static void main(String[] args) {
//        String url = "https://api.xiaomaigui.com/Rscansub/receiveReqData.html";
//        JSONObject jsonObject = new JSONObject();
//        try {
//            long timestamp = System.currentTimeMillis() / 1000;
//            HttpParamers httpParamers = new HttpParamers(HttpMethod.POST);
//            httpParamers.addParam("mobile", "15959445322");
//            httpParamers.addParam("source", "2");
//            httpParamers.addParam("orderNo", "SLJ" + DateUtils.format(new Date(), DateUtils.YMDHMSS));
//            //httpParamers.addParam("containerid", "869012040985959");
//            httpParamers.addParam("containerid", "863617067632213");
//            httpParamers.addParam("timestamp", timestamp);
//            httpParamers.addParam("appid", APPID);
//            httpParamers.addParam("method", "open");
//            // 加密规则
//            String ciphertext = "{\"timestamp\":" + timestamp + ",\"appid\":\"" + APPID + "\"}";
//            ciphertext = RSAUtil.encrypt(ciphertext, PUBLIC_KEY);
//            httpParamers.addParam("ciphertext", ciphertext);
//            httpParamers.setJsonParamer();
//            String resultService = HttpClient.doService(url, httpParamers, null);
//            logger.info("小卖柜开门接口返回信息-{}", resultService);
//            if (StringUtils.isNotBlank(resultService)) {
//                jsonObject = JSONObject.parseObject(resultService);
//            }
//        } catch (Throwable e) {
//            logger.info("小卖柜开门接口返回信息-{}", e.getMessage());
//        }
//        System.out.println(jsonObject);
        //System.out.println(open("863617067632213", "SLJ" + DateUtils.format(new Date(), DateUtils.YMDHMSS), "15959445322"));
        System.out.println(orderNotify("863617067632213", "SLJ20220414120458302", "SLJ20220414153126268", new BigDecimal("19.000"), DateUtils.format(new Date(1649921499 * 1000), DateUtils.Y_M_D_H_M_S)));
    }
}
